//
//  HeaderView.swift
//  Duyiguqin
//
//  Created by 张昭 on 2024/8/4.
//

import UIKit

class TiaoyinHeaderView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var canvasView: TiaoyinCanvasView = {
        let v = TiaoyinCanvasView()
        return v
    }()
    
    private lazy var leftTitleLabel: UILabel = {
        let v = UILabel()
        v.text = "当前音"
        v.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        v.textColor = .black
        return v
    }()
    
    private lazy var rightTitleLabel: UILabel = {
        let v = UILabel()
        v.text = "标准音"
        v.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        v.textColor = .black
        return v
    }()
    
    private lazy var leftValueLabel: UILabel = {
        let v = UILabel()
        v.text = "0"
        v.font = UIFont.systemFont(ofSize: 23, weight: .bold)
        v.textColor = UIColor(0xD3C390)
        v.textAlignment = .right
        v.adjustsFontSizeToFitWidth = true
        return v
    }()
    
    private lazy var rightValueLabel: UILabel = {
        let v = UILabel()
        v.text = "0"
        v.font = UIFont.systemFont(ofSize: 23, weight: .bold)
        v.textColor = UIColor(0xD3C390)
        v.textAlignment = .left
        v.adjustsFontSizeToFitWidth = true
        return v
    }()
    
    private lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(0xD3C390)
        return v
    }()
    
    private lazy var middleValueLabel: UILabel = {
        let v = UILabel()
        v.text = "*"
        v.font = UIFont.systemFont(ofSize: 28, weight: .bold)
        v.textColor = UIColor(0xD3C390)
        v.textAlignment = .center
        return v
    }()
    
    private var stardardHZ: Double = 0
}

extension TiaoyinHeaderView {
    
    private func initializeSubviews() {
        backgroundColor = .white
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.05
        layer.shadowOffset = CGSize(width: 0, height: 5)
        layer.shadowRadius = 15
        layer.cornerRadius = 10
        clipsToBounds = false
        
        addSubview(canvasView)
        canvasView.snp.makeConstraints { make in
            make.left.right.top.equalTo(0)
            make.height.equalTo(200)
        }
        
        addSubview(leftTitleLabel)
        leftTitleLabel.snp.makeConstraints { make in
            make.right.equalTo(self.snp.centerX).offset(-20)
            make.top.equalTo(canvasView.snp.bottom).offset(20)
        }
        
        addSubview(rightTitleLabel)
        rightTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(self.snp.centerX).offset(20)
            make.top.equalTo(leftTitleLabel)
        }
        
        addSubview(leftValueLabel)
        leftValueLabel.snp.makeConstraints { make in
            make.right.equalTo(self.snp.centerX).offset(-20)
            make.top.equalTo(rightTitleLabel.snp.bottom).offset(10)
            make.left.lessThanOrEqualToSuperview().offset(5)
        }
        
        addSubview(rightValueLabel)
        rightValueLabel.snp.makeConstraints { make in
            make.left.equalTo(self.snp.centerX).offset(20)
            make.top.equalTo(leftValueLabel)
            make.right.lessThanOrEqualToSuperview().offset(-5)
        }
        
        addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.width.equalTo(1)
            make.centerX.equalToSuperview()
            make.top.equalTo(leftTitleLabel.snp.bottom)
            make.bottom.equalTo(leftValueLabel.snp.lastBaseline)
        }
        
        addSubview(middleValueLabel)
        middleValueLabel.snp.makeConstraints { make in
            make.top.equalTo(leftValueLabel.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-20)
        }
    }
    
    func update(HZ value: Double) {
        let rate = 60 / 15.0
        var result: Double = -90
        if value < stardardHZ {
            result = (value - stardardHZ) * rate - 90
            result = max(-150, result)
        } else if value == stardardHZ {
            result = -90
        } else {
            result = (value - stardardHZ) * rate - 90
            result = min(result, -30)
        }
        canvasView.setValue(result)
        leftValueLabel.text = String(format: "%.2f", max(0, value))
    }
    
    func update(Xian value: Int) {
        middleValueLabel.text = "\(value)弦"
    }
    
    func update(SHZ value: Double) {
        stardardHZ = value
        rightValueLabel.text = String(format: "%.2f", value)
    }
}
